const router = require('../routes')

// 设置校验函数
module.exports = (validator) => {
  return (req, res, next) => {
    const { error, value } = validator(req.body)
    if (error) {
      // 说明数据不满足于规则
      return res.status(400).json({
        code: 400,
        value: error._original,
        msg: error.details[0].message
      })
    } else {
      req.validValue = value
      next()
    }
  }
}
